﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;

namespace JY.Wpf.Core.CaseForCaliburn
{
    /// <summary>
    /// Coroutine(协同程序)扩展
    /// </summary>
    public static class CoroutineExtension
    {
        #region # 执行一个协同程序——void BeginExecute(IResult coroutine...
        /// <summary>
        /// 执行一个协同程序
        /// </summary>
        /// <param name="coroutine">待执行的协同程序</param>
        /// <param name="context">The context to execute the coroutine within.</param>
        /// /// <param name="callback">The completion callback for the coroutine.</param>
        public static void BeginExecute(IResult coroutine, CoroutineExecutionContext context = null, EventHandler<ResultCompletionEventArgs> callback = null)
        {
            List<IResult> results = new List<IResult> { coroutine };
            Coroutine.BeginExecute(results.GetEnumerator(), context, callback);
        } 
        #endregion

        /// <summary>
        /// 异步执行一个协同程序
        /// </summary>
        /// <param name="coroutine">The coroutine to execute.</param>
        /// <param name="context">The context to execute the coroutine within.</param>
        /// <returns>A task that represents the asynchronous coroutine.</returns>
        public static Task ExecuteAsync(IResult coroutine, CoroutineExecutionContext context = null)
        {
            List<IResult> results = new List<IResult> { coroutine };
            return Coroutine.ExecuteAsync(results.GetEnumerator(), context);
        }
    }
}
